package com.qst.controller;


import com.qst.domain.Book;
import com.qst.domain.User;
import com.qst.service.BookService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/book")
public class BookController {
    // 注入service对象
    @Autowired
    private BookService bookService;

    @RequestMapping("/selectNewBooks")
    public ModelAndView selectNewBooks(){
        // 查询最新上架的5本书
        int pageNum = 1;
        int pageSize = 5;
        PageResult result = bookService.selectNewBooks(pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        // 添加数据
        mv.addObject("pageResult",result);
        // 添加视图
        mv.setViewName("books_new");
        return mv;
    }

    /*
根据id查询图书
*/
    @RequestMapping("/findById")
    @ResponseBody
    public Result<Book> findById(String id){
        Book book = bookService.findById(id);
        if (book==null){
            return new Result<>(false,"查询图书不存在!");
        }
        return new Result<>(true,"查询图书成功!",book);
    }

    /*
    借阅图书
    */
    @RequestMapping("/borrowBook")
    @ResponseBody
    public Result borrowBook(Book book, HttpSession session){
        // 获取当前登录的用户名
        String name = ((User) session.getAttribute("USER_SESSION")).getUsername();
        // 将图书借阅人修改成用户名
        book.setBorrower(name);
        // 根据图书id和用户进行借阅图书
        Integer i = bookService.borrowBook(book);
        if (i!=1){
            return new Result(false,"借书失败!");
        }
        return new Result(true,"借书成功,请到行政中心取书!");
    }

    /*
	分页查询复合条件且未下架的图书信息
	*/
    @RequestMapping("/search")
    public ModelAndView search(Book book, Integer pageNum, Integer pageSize, HttpServletRequest request){
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=10;
        }
        // 查询到的图书信息
        PageResult pageResult = bookService.search(book, pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("books");
        // 将查询到的数据存放在ModelAndView对象中
        mv.addObject("pageResult",pageResult);
        // 将查询的参数返回到页面中,用于回显到查询的输入框中
        mv.addObject("search",book);
        // 将当前页码数返回到页面中,用于分页插件的分页显示
        mv.addObject("pageNum",pageNum);
        // 将当前查询的控制器路径返回到页面,页码变化时,继续向该路径发送请求
        mv.addObject("gourl",request.getRequestURI());
        return mv;
    }

    @ResponseBody
    @RequestMapping("/addBook")
    public Result addBook(Book book){
        Integer integer = bookService.addBook(book);
        if (integer!=1){
            return new Result(false,"新增图书失败");
        }
        return new Result(true,"新增图书成功!");
    }

    /*
	编辑图书方法
*/
    @ResponseBody
    @RequestMapping("/editBook")
    public Result editBook(Book book){
        Integer r = bookService.editBook(book);
        if (r!=1){
            return  new Result(false,"修改失败");
        }
        return new Result(true,"修改成功");
    }

    /*
分页查询当前被借阅且未归还的图书信息
*/
    @RequestMapping("/searchBorrowed")
    public ModelAndView searchBorrowed(Book book,Integer pageNum,Integer pageSize,HttpServletRequest request){
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        // 获取当前登录的用户
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        PageResult result = bookService.searchBorrowed(book, user, pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        // 设置视图
        mv.setViewName("book_borrowed");
        // 将查询到的数据存放在ModelAndView对象中
        mv.addObject("pageResult",result);
        // 将查询的参数返回到页面,用于回显到查询的输入框
        mv.addObject("search",book);
        // 将当前页码返回到页面,用于分页插件的分页显示
        mv.addObject("pageNum",pageNum);
        mv.addObject("gourl",request.getRequestURI());
        return mv;
    }

    /**
     * 归还图书
     */
    @RequestMapping("/returnBook")
    @ResponseBody
    public Result returnBook(String id,HttpSession session){
        // 获取当前用户信息
        User user = (User)session.getAttribute("USER_SESSION");
        boolean b = bookService.returnBook(id, user);
        if (!b){
            return new Result(false,"还书失败");
        }
        return new Result(true,"还书确认中,请先到行政中心还书!");
    }

    /*
图书归还确认
*/
    @RequestMapping("/returnConfirm")
    @ResponseBody
    public Result returnConfirm(String id){
        Integer integer = bookService.returnConfirm(id);
        if (integer!=1){
            return new Result(false,"确认失败!");
        }
        return new Result(true,"确认成功!");
    }
}